﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DevExpress.Web.ASPxEditors;
using DevExpress.Web.ASPxGridView;

namespace UdrinkWater_Project.Admin
{
    public partial class Accounting : System.Web.UI.Page
    {
        string Connection = ConfigurationManager.ConnectionStrings["DB_Udrink"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {
            DateEdit_Withdraw.Value = DateTime.Now;
            DateEdit_Ex.Value = DateTime.Now;
        }
        protected void bt_RevFind_Click(object sender, EventArgs e)
        {
            if ((CB_Month.Value == "01") || (CB_Month.Value == "03") || (CB_Month.Value == "05") || (CB_Month.Value == "07") || (CB_Month.Value == "08") || (CB_Month.Value == "10") || (CB_Month.Value == "12"))
            {
               Session["Date"] = Convert.ToDateTime("1/" + CB_Month.Value + "/" + CB_Year.Value);
            Session["Date1"] = Convert.ToDateTime("31/" + CB_Month.Value + "/" + CB_Year.Value); 
            }
            else if ((CB_Month.Value == "04") || (CB_Month.Value == "06") || (CB_Month.Value == "09") || (CB_Month.Value == "11"))
            {
                Session["Date"] = Convert.ToDateTime("1/" + CB_Month.Value + "/" + CB_Year.Value);
                Session["Date1"] = Convert.ToDateTime("30/" + CB_Month.Value + "/" + CB_Year.Value);
            }
            else
            {
                CheckTime ct = new CheckTime();
                DataTable dt = new DataTable();
                dt = ct.CheckDayOfYear((Convert.ToInt32(CB_Year.Value) - 543).ToString());
                string Year = dt.Rows[0]["DayCount"].ToString();
                if (Year=="365")
                {
                    Session["Date"] = Convert.ToDateTime("1/" + CB_Month.Value + "/" + CB_Year.Value);
                    Session["Date1"] = Convert.ToDateTime("28/" + CB_Month.Value + "/" + CB_Year.Value);
                }
                if (Year == "366")
                {
                    Session["Date"] = Convert.ToDateTime("1/" + CB_Month.Value + "/" + CB_Year.Value);
                    Session["Date1"] = Convert.ToDateTime("29/" + CB_Month.Value + "/" + CB_Year.Value);
                }
            }
            
        }

        protected void GV_Revenue_CellEditorInitialize(object sender, ASPxGridViewEditorEventArgs e)
        {
            if (e.Column.FieldName == "Revenue_Date")
            {
                e.Editor.Value = DateTime.Now;
               // Session["Datetime"] = DateTime.Now;
            }
        }

        protected void GV_Revenue_RowInserting(object sender, DevExpress.Web.Data.ASPxDataInsertingEventArgs e)
        {
           // e.NewValues[""] = 
        }

        protected void btOK_Click(object sender, EventArgs e)
        {
            InsertsRiv();
            GV_Revenue.DataBind();
        }
        public static object GetDataValue(object value)
        {
            if (value == null)
            {
                return DBNull.Value;
            }

            return value;
        }
        public void InsertsRiv()
        {
            string sqltext = @"insert into [TB_Revenue] 
                                   ( [Revenue_Date]
                                    ,[Revenue_List]
                                    ,[Revenue_Price])
                                   values (@Date,@List,@Price)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            //string[] datesplit = DateEdit_Withdraw.Value.ToString().Split(' ');
            //string Time = DateTime.Now.ToString("HH:mm", new System.Globalization.CultureInfo("th-TH"));
            //string DateT = datesplit[0] + " " + Time;
            //string DateNow = Convert.ToDateTime(DateT).ToString("yyyy/MM/dd HH:mm", new System.Globalization.CultureInfo("en-US"));
            //string Date = Convert.ToDateTime(DateEdit_Withdraw.Value).ToString("yyyy/MM/dd", new System.Globalization.CultureInfo("en-US"));
            sqlcomm.Parameters.AddWithValue("@Date", GetDataValue(DateEdit_Withdraw.Value));
            sqlcomm.Parameters.AddWithValue("@List", GetDataValue(tbNum0.Text));
            sqlcomm.Parameters.AddWithValue("@Price", GetDataValue(tbNum.Text));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }
        public void InsertsEx()
        {
            string sqltext = @"insert into [TB_Expenditure] 
                                   ( [Expenditure_Date]
                                    ,[Expenditure_List]
                                    ,[Expenditure_Price])
                                   values (@DateEx,@ListEx,@PriceEx)";
            SqlCommand sqlcomm = new SqlCommand();
            SqlConnection sqlconn = new SqlConnection(Connection);
            sqlcomm.Connection = sqlconn;
            sqlcomm.CommandType = System.Data.CommandType.Text;
            sqlcomm.CommandText = sqltext;
            //string[] datesplit = DateEdit_Withdraw.Value.ToString().Split(' ');
            //string Time = DateTime.Now.ToString("HH:mm", new System.Globalization.CultureInfo("th-TH"));
            //string DateT = datesplit[0] + " " + Time;
            //string DateNow = Convert.ToDateTime(DateT).ToString("yyyy/MM/dd HH:mm", new System.Globalization.CultureInfo("en-US"));
            //string Date = Convert.ToDateTime(DateEdit_Withdraw.Value).ToString("yyyy/MM/dd", new System.Globalization.CultureInfo("en-US"));
            sqlcomm.Parameters.AddWithValue("@DateEx", GetDataValue(DateEdit_Ex.Value));
            sqlcomm.Parameters.AddWithValue("@ListEx", GetDataValue(tb_ListEx.Text));
            sqlcomm.Parameters.AddWithValue("@PriceEx", GetDataValue(tb_PriceEx.Text));

            try
            {
                sqlcomm.Connection.Open();
                sqlcomm.ExecuteNonQuery();
            }
            catch (Exception)
            {

                sqlcomm.Connection.Close();
                sqlcomm.Connection.Dispose();
            }
            sqlcomm.Connection.Close();
            sqlcomm.Connection.Dispose();

        }

        protected void btOK0_Click(object sender, EventArgs e)
        {
            InsertsEx();
            GV_Expenditure.DataBind();
        }

       

       

        
    }
}